package com.example.chatapp.controller;

import com.example.chatapp.common.ApiResponse;
import com.example.chatapp.manager.RedisCacheManager;
import com.example.chatapp.service.SeqService;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * 管理控制器
 * 职责：提供管理和监控接口
 */
@RestController
@RequestMapping("/admin")
public class AdminController {
    
    private final RedisCacheManager cacheManager;
    private final SeqService seqService;
    
    public AdminController(RedisCacheManager cacheManager, SeqService seqService) {
        this.cacheManager = cacheManager;
        this.seqService = seqService;
    }
    
    /**
     * 清理指定房间的所有 Redis 数据
     */
    @DeleteMapping("/clear-room")
    public ApiResponse<Map<String, Object>> clearRoom(@RequestParam String roomId) {
        // 清理序列号
        cacheManager.clearSeq(roomId);
        
        // 清理最近消息列表
        cacheManager.clearRecentMessages(roomId);
        
        Map<String, Object> result = new HashMap<>();
        result.put("roomId", roomId);
        result.put("clearedKeys", new String[]{"chat:seq:" + roomId, "chat:recent:" + roomId});
        
        return ApiResponse.success("房间数据清理成功", result);
    }
    
    /**
     * 查看 Redis 中的所有聊天相关键
     */
    //list/keys显示内容为{
    //  "seqKeys": [
    //    "chat:seq:default",
    //    "chat:seq:1"
    //  ],
    //  "channelKeys": [],
    //  "recentKeys": [
    //    "chat:recent:1",
    //    "chat:recent:default"
    //  ],
    //  "status": "OK"
    //}
    @GetMapping("/list-keys")
    public ApiResponse<Map<String, Object>> listKeys() {
        Set<String> seqKeys = cacheManager.keys("chat:seq:*");
        Set<String> recentKeys = cacheManager.keys("chat:recent:*");
        Set<String> channelKeys = cacheManager.keys("chat:channel:*");
        
        Map<String, Object> result = new HashMap<>();
        result.put("seqKeys", seqKeys);
        result.put("recentKeys", recentKeys);
        result.put("channelKeys", channelKeys);
        
        return ApiResponse.success(result);
    }
    
    /**
     * 查看指定房间的详细信息
     * {
     *   "currentSeq": "597",
     *   "recentMessageCount": 100,
     *   "roomId": "default",
     *   "status": "OK"
     * }
     */
    @GetMapping("/room-info")
    public ApiResponse<Map<String, Object>> roomInfo(@RequestParam String roomId) {
        // 获取当前 seq
        Long currentSeq = seqService.currentSeq(roomId);
        
        // 获取最近消息数量
        java.util.List<Object> recentMessages = cacheManager.getRecentMessages(roomId, 100);
        
        Map<String, Object> result = new HashMap<>();
        result.put("roomId", roomId);
        result.put("currentSeq", currentSeq != null ? currentSeq.toString() : "0");
        result.put("recentMessageCount", recentMessages.size());
        
        return ApiResponse.success(result);
    }
    
    /**
     * 查看订阅状态（调试用）
     */
    @GetMapping("/subscriptions")
    public ApiResponse<Map<String, Object>> getSubscriptions() {
        Map<String, Object> result = new HashMap<>();
        // 这里需要注入 RedisMessageQueueManager 或 ChatService
        result.put("message", "Please check server logs for subscription details");
        return ApiResponse.success(result);
    }
}
